package weixinkeji.dev.h;

import java.util.HashSet;
import java.util.Set;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.ann.power.bean.IMYUserPowerSet;
import sys.wx.c.ATR;
import sys.wx.c.components.cv.CV;
import sys.wx.m.db.SQL.mysql.common.TJF;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.SQL.mysql.common.where.WhereConfig;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.xy.XY;
import weixinkeji.dev.h.check.LoginCheck;
import weixinkeji.dev.h.vo.LoginMsg;
import weixinkeji.dev.power.center.bean.SysP;
import weixinkeji.dev.power.center.service.URPService;
import weixinkeji.dev.power.user.bean.SysUser;

/**
 *
 * @author IK
 */
@H("sys/login")
public class Login {

    ATR a;

    public Login(ATR a) {
        this.a = a;
    }

    @M("/user/login")//sys/login/user/login.imy
    public String userLogin() {
        LoginMsg lm = CV.getObjectByFields(LoginMsg.class, a.request, new LoginCheck().Login());
        if (null == lm) {
            a.session.removeAttribute(XY.SAFE_CODE);
            return null;
        }
        if (null == a.session.getAttribute(XY.SAFE_CODE)) {
            a.printOne(4);
            return null;
        }

        if (!a.session.getAttribute(XY.SAFE_CODE).equals(lm.code)) {
            a.printOne(3);
            return null;
        }
        WhereConfig config = new WhereConfig(SysUser.class, "");
        Where tj = TJF.getWhere(config);
        tj.put(Where.TJ_IGNORE_NOTHING, "", "user_account", "=", lm.account)
                .put(Where.TJ_IGNORE_NOTHING, Where.AND, "user_password", "=", lm.password)
                .put(Where.TJ_IGNORE_NOTHING, Where.AND, "style", "=", 2);
//        tj.put("user_account", "=", lm.account).putAND("user_password", "=", lm.password).putAND("style", "=", 2);
        SysUser su = Dao.selectOneByTJ(SysUser.class, tj.getWhere());
        if (null == su) {
            a.printOne(1);
            return null;
        }
        a.session.removeAttribute(XY.SAFE_CODE);
        a.session.setAttribute(XY.SESSION_LOGIN, su);
        Set<String> set = new HashSet();
        for (SysP obj : URPService.getURByUserID(su.getUser_id())) {
            set.add(obj.getPower_id());
        }
        a.session.setAttribute(XY.LOGIN_POWER, new IMYUserPowerSet(set, su.getSort()));

        a.printOne(su.getSort() == 2 ? 2 : -1);
        return null;///
    }
}
